//
//  CPHomeHeader.m
//  CPApp
//
//  Created by 健健 on 2018/3/21.
//  Copyright © 2018年 Class. All rights reserved.
//

#import "CPHomeHeader.h"
@interface CPHomeHeader ()
@property(nonatomic,strong)TZButtonsHeaderView * headerBts;
@property(nonatomic,strong) NSMutableArray * arrangMutableArr;
@end
@implementation CPHomeHeader

-(NSMutableArray *)arrangMutableArr {
    if (!_arrangMutableArr) {
        _arrangMutableArr =[NSMutableArray array];
    }
    return _arrangMutableArr;
}

-(instancetype)init {
    if (self == [super init]) {
        self = [[NSBundle mainBundle] loadNibNamed:@"CPHomeHeader" owner:nil options:nil].firstObject;        
        [self configHeaderBtns];
    }
    return self;
}

-(void)setArrangementArr:(NSArray *)arrangementArr {
    _arrangementArr = arrangementArr;
    for (int i =0; i<arrangementArr.count; i++) {
        UILabel * label = [self configLabelWithIndex:i];
        label.text = arrangementArr[i];
        label.hidden = NO;
    }
    for (NSInteger j = arrangementArr.count; j<self.arrangMutableArr.count; j++) {
        UILabel * label = [self configLabelWithIndex:j];
        label.hidden = YES;
    }
    [self setNeedsLayout];
}
-(UILabel *)configLabelWithIndex:(NSInteger)index {
    UILabel * label;
    if (index<self.arrangMutableArr.count) {
        label = self.arrangMutableArr[index];
    }else {
        label = [UILabel new];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = index;
        label.layer.masksToBounds = YES;
        label.layer.cornerRadius = 10;
        label.backgroundColor = HJColor(240, 8, 7);
        [self.arrangMutableArr addObject:label];
        [self.ballView addSubview:label];
    }
    return label;
}
-(void)layoutSubviews {
    [super layoutSubviews];
    float W = 20;
    for (int i = 0; i<self.arrangMutableArr.count; i++) {
        UILabel *label = [self configLabelWithIndex:i];
        label.frame = CGRectMake(i*(W + 5), 0, W, W);
    }
}
-(void)setTitles:(NSMutableArray *)titles {
    _titles = titles;
    self.headerBts.titles = titles;
    self.headerBts.selectBtnIndex = 0;
    self.headerBts.contentSize = CGSizeMake(titles.count * 110 , 40);
}
-(void)setShowSpots:(NSMutableArray *)showSpots {
    _showSpots= showSpots;
    self.headerBts.showSpots = showSpots;
}
-(void)configHeaderBtns {
    self.headerBts = [[TZButtonsHeaderView alloc]init];
    self.headerBts.frame = CGRectMake(0, 0, self.headTitleView.width, self.headTitleView.height);
    self.headerBts.shouldSelect = YES;
    self.headerBts.btnWidth = 110;
    self.headerBts.changeFontWhenSelected = YES;
    MJWeakSelf
    [self.headerBts setDidClickButtonWithIndex:^(TZBaseButton *btn, NSInteger index) {
        if (index>1 &&index<weakSelf.titles.count-2.5) {
            weakSelf.headerBts.contentOffset = CGPointMake(weakSelf.headerBts.btnWidth *(index -2) +weakSelf.headerBts.btnWidth/4, 0);
        }else if (index < 2) {
            weakSelf.headerBts.contentOffset = CGPointMake(0, 0);
        }else {
            weakSelf.headerBts.contentOffset = CGPointMake(weakSelf.headerBts.btnWidth *(weakSelf.titles.count-2), 0);
        }
        [weakSelf loadWithIndex:index];
    }];
    [self.headTitleView addSubview:self.headerBts];
}
-(void)setNavTitle:(NSString *)navTitle {
    _navTitle = navTitle;
}
-(void)loadWithIndex:(NSInteger)index {
    NSString * playtype;
    NSString * lottype;
    if ([_navTitle isEqualToString:@"双色球"]) {
        switch (index) {
            case 0:
                playtype = @"1039";
                break;
            case 1:
                playtype = @"1040";
                break;
            case 2:
                playtype = @"1041";
                break;
            case 3:
                playtype = @"1045";
                break;
            case 4:
                playtype = @"1046";
                break;
            case 5:
                playtype = @"1047";
                break;
            case 6:
                playtype = @"1037";
                break;
            default:
                break;
        }
        lottype = @"1001";
    }else if ([_navTitle isEqualToString:@"福彩3D"]) {
        switch (index) {
            case 0:
                playtype = @"1038";
                break;
            case 1:
                playtype = @"1042";
                break;
            case 2:
                playtype = @"1043";
                break;
            case 3:
                playtype = @"1044";
                break;
            case 4:
                playtype = @"1050";
                break;
            case 5:
                playtype = @"1051";
                break;
            case 6:
                playtype = @"1052";
                break;
            default:
                break;
        }
        lottype = @"1002";
    }else if ([_navTitle isEqualToString:@"七乐彩"]) {
        switch (index) {
            case 0:
                playtype = @"1039";
                break;
            case 1:
                playtype = @"1040";
                break;
            case 2:
                playtype = @"1041";
                break;
            case 3:
                playtype = @"1042";
                break;
            case 4:
                playtype = @"1043";
                break;
            default:
                break;
        }
        lottype = @"1003";
    }else if ([_navTitle isEqualToString:@"七星彩"]) {
        switch (index) {
            case 0:
                playtype = @"1055";
                break;
            case 1:
                playtype = @"1056";
                break;
            case 2:
                playtype = @"1057";
                break;
            case 3:
                playtype = @"1058";
                break;
            case 4:
                playtype = @"1059";
                break;
            case 5:
                playtype = @"1060";
                break;
            case 6:
                playtype = @"1061";
                break;
            default:
                break;
        }
        lottype = @"1004";
    }else if ([_navTitle isEqualToString:@"排列三"]) {
        switch (index) {
            case 0:
                playtype = @"1038";
                break;
            case 1:
                playtype = @"1042";
                break;
            case 2:
                playtype = @"1043";
                break;
            case 3:
                playtype = @"1044";
                break;
            case 4:
                playtype = @"1050";
                break;
            case 5:
                playtype = @"1051";
                break;
            case 6:
                playtype = @"1052";
                break;
            default:
                break;
        }
        lottype = @"1005";
    }else if ([_navTitle isEqualToString:@"排列五"]) {
        switch (index) {
            case 0:
                playtype = @"1050";
                break;
            case 1:
                playtype = @"1051";
                break;
            case 2:
                playtype = @"1052";
                break;
            case 3:
                playtype = @"1053";
                break;
            case 4:
                playtype = @"1054";
                break;
            default:
                break;
        }
        lottype = @"1006";
    }else {// 大乐透
        switch (index) {
            case 0:
                playtype = @"1039";
                break;
            case 1:
                playtype = @"1040";
                break;
            case 2:
                playtype = @"1041";
                break;
            case 3:
                playtype = @"1042";
                break;
            case 4:
                playtype = @"1043";
                break;
            case 5:
                playtype = @"1048";
                break;
            case 6:
                playtype = @"1049";
                break;
            default:
                break;
        }
        lottype = @"1007";
    }
    [mNotificationCenter postNotificationName:@"headerBtnNotification" object:nil userInfo:@{@"index":playtype,@"lottype":lottype}];
}



- (IBAction)gameShowBtnClicked:(UIButton *)sender {
    [mNotificationCenter postNotificationName:@"gameShowBtnNotification" object:nil];
}
@end
